//
//  HomeMeasureButton.swift
//  DaRings
//
//  Created by 尹琼 on 2023/3/2.
//

import UIKit
import MYModule
import RxSwift
import RxCocoa

class HomeMeasureButton: Button {
    
    let closeButton = Button(type: .custom)
    
    private(set) var selectedTitle: String?
    
    private(set) var animationTitle: String = ""
    
    private(set) var animationlabel = Label()
    
    private(set) var index: Int = 0
    
    private(set) var timer: Timer?
   
    
    override func makeUI() {
        super.makeUI()
        
        closeButton.isUserInteractionEnabled = false
        closeButton.setImage(UIImage(named: "ic_close_n"), for: .normal)
        
        animationlabel.font = self.titleLabel?.font
        animationlabel.isHidden = true
        
        addSubview(animationlabel)
        addSubview(closeButton)
        self.bringSubviewToFront(closeButton)
        
        animationlabel.snp.makeConstraints { make in
            make.left.equalTo(self.titleLabel!.snp.right)
            make.bottom.equalTo(self.titleLabel!.snp.bottom)
            make.top.equalTo(self.titleLabel!.snp.top)
        }
        
        closeButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-28)
        }
        
   
    }
    
    
    override func setTitle(_ title: String?, for state: UIControl.State) {
        super.setTitle(title, for: state)
        
        if self.selectedTitle == nil && state == .selected {
            self.selectedTitle = title
        }
    }
    
    func changeSelectedTitle() {
        
        self.closeButton.isHidden = false
        
        var index = self.index
        if index > 3 {
            index = 0
        }
        self.animationSlectedTitle(selectedTitle: self.selectedTitle, index: index)
        
        animationlabel.layoutIfNeeded()
        self.layoutIfNeeded()
        
        
    }
    
    public func startAnimation() {
        
        changeSelectedTitle()
        addTimer()
        
    }
    

    func stopAnimation() {
        
        self.closeButton.isHidden = true
        normalSlectedTitle()
        removeTimer()
    }
    

    override var isSelected: Bool {
        
        didSet {
            
            if self.state == .normal {
                return
            }
            
            if isSelected == true {
                
                startAnimation()
                
            } else {
               
                stopAnimation()
                
            }
        }
        
    }
    
    override var isEnabled: Bool {
        
        didSet {
            
            stopAnimation()
            
        }
        
    }
    

    
    func animationSlectedTitle(selectedTitle: String?, index: Int) {
        
        if index > 0 {
            self.animationTitle = self.animationTitle + "."
            
        } else {
            self.animationTitle = ""
        }
        
        let animationTitle = self.animationTitle
        
        animationlabel.isHidden = false
        animationlabel.font = self.titleLabel?.font
        animationlabel.textColor = self.titleColor(for: .selected)
        animationlabel.text = animationTitle
        self.index = index + 1
        
    }
    
    func normalSlectedTitle() {
        
        self.setTitle(selectedTitle, for: .selected)
        self.index = 0
        self.animationTitle = ""
        animationlabel.text = nil
        animationlabel.isHidden = true
        animationlabel.layoutIfNeeded()
        self.layoutIfNeeded()
        
    }
    
    func addTimer() {
        
        if let _ = self.timer {
            self.removeTimer()
        }
        let timer = Timer.scheduledTimer(withTimeInterval: 0.3, repeats: true) {[weak self] _ in
            
            self?.changeSelectedTitle()
        }
        
        RunLoop.current.add(timer, forMode: .common)
        
        self.timer = timer
        
    }
    
    func removeTimer() {
        
        self.timer?.invalidate()
        self.timer = nil
        
    }
    
    
}

extension HomeMeasureButton {
    
    enum AnimationType {
        case normal
        case animationing
        case endAnimation
    }
    
}
